package com.eleadmin.common.system.controller;

import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.system.service.LeavemessageService;
import com.eleadmin.common.system.entity.Leavemessage;
import com.eleadmin.common.system.param.LeavemessageParam;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.BatchParam;
import com.eleadmin.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 控制器
 *
 * @author EleAdmin
 * @since 2024-02-26 22:09:04
 */
@Api(tags = "留言管理")
@RestController
@RequestMapping("/system/leavemessage")
public class LeavemessageController extends BaseController {
    @Resource
    private LeavemessageService leavemessageService;

//    @PreAuthorize("hasAuthority('system:leavemessage:list')")
    @OperationLog
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ApiResult<PageResult<Leavemessage>> page(LeavemessageParam param) {
        PageParam<Leavemessage, LeavemessageParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(leavemessageService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(leavemessageService.pageRel(param));
    }

//    @PreAuthorize("hasAuthority('system:leavemessage:list')")
    @OperationLog
    @ApiOperation("查询全部")
    @GetMapping()
    public ApiResult<List<Leavemessage>> list(LeavemessageParam param) {
        PageParam<Leavemessage, LeavemessageParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(leavemessageService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(leavemessageService.listRel(param));
    }

//    @PreAuthorize("hasAuthority('system:leavemessage:list')")
    @OperationLog
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ApiResult<Leavemessage> get(@PathVariable("id") Integer id) {
        return success(leavemessageService.getById(id));
        // 使用关联查询
        //return success(leavemessageService.getByIdRel(id));
    }

//    @PreAuthorize("hasAuthority('system:leavemessage:save')")
    @OperationLog
    @ApiOperation("添加")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Leavemessage leavemessage) {
        if (leavemessageService.save(leavemessage)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('system:leavemessage:update')")
    @OperationLog
    @ApiOperation("修改")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Leavemessage leavemessage) {
        if (leavemessageService.updateById(leavemessage)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('system:leavemessage:remove')")
    @OperationLog
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (leavemessageService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('system:leavemessage:save')")
    @OperationLog
    @ApiOperation("批量添加")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Leavemessage> list) {
        if (leavemessageService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('system:leavemessage:update')")
    @OperationLog
    @ApiOperation("批量修改")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Leavemessage> batchParam) {
        if (batchParam.update(leavemessageService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('system:leavemessage:remove')")
    @OperationLog
    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (leavemessageService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
